﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;

namespace BuXiaChu.OrderDishes.Web.Admin.Restaurant
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class DishesCat : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            var type = Request.Form["t"];
            switch (type)
            {
                case "2":
                    Add();
                    break;
                case "3":
                    Update();
                    break;
                case "4":
                    Delete();
                    break;
                default: break;
            }
        }

        private void Add()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            var name = GetFormValue("Name");
            var restaurantId = GetFormValue("RestaurantId");
            var iRestaurantId = 0;
            if (!string.IsNullOrEmpty(name)
                && int.TryParse(restaurantId, out iRestaurantId))
            {
                DishesCatModel entity = new DishesCatModel();
                entity.Name = name.Trim();
                entity.RestaurantID = iRestaurantId;

                result.success = RestaurantManager.Save(entity);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private void Update()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            var name = GetFormValue("Name");
            var restaurantId = GetFormValue("RestaurantId");
            var iRestaurantId = 0;
            var catId = GetFormValue("id");
            var iCatId = 0;
            if (!string.IsNullOrEmpty(name)
                && int.TryParse(restaurantId, out iRestaurantId)
                && int.TryParse(catId,out iCatId))
            {
                DishesCatModel entity = new DishesCatModel();
                entity.CatID = iCatId;
                entity.Name = name.Trim();
                entity.RestaurantID = iRestaurantId;

                result.success = RestaurantManager.Save(entity);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private void Delete()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            var catId = GetFormValue("id");
            var iCatId = 0;
            if (int.TryParse(catId, out iCatId))
            {
                 result.success = RestaurantManager.DeleteDishesCatById(iCatId);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
